package com.yang;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by dllo on 18/8/27.
 * ░░░░░░░░░░░░░░░░░░░░░░░░▄░░
 * ░░░░░░░░░▐█░░░░░░░░░░░▄▀▒▌░
 * ░░░░░░░░▐▀▒█░░░░░░░░▄▀▒▒▒▐
 * ░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
 * ░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
 * ░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
 * ░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒
 * ░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 * ░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄
 * ░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒
 * ▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒
 * My Dear Taoism's Friend .Please SitDown.
 */
public class MainTest {

    @Test
    public void test1(){
        String html = "<html><head><title>这是一个标题</title></head>" +
                "<body><p>测试</p></body></html>";

        Document document=Jsoup.parse(html);
        System.out.println(document.title());
        System.out.println(document);

    }

    @Test
    public  void test2() throws IOException {
        //通过url获取document
       Document document= Jsoup.connect("https://www.csdn.net/").get();
        System.out.println(document);
    }
    @Test
    public void test3() throws IOException {
        File file = new File("/Users/dllo/Desktop/GitDemo/spider/src/main/java/com/home.html");
     Document document =   Jsoup.parse(file,"UTF-8");
        System.out.println(document.title());

       Elements pTags=  document.getElementsByTag("p");
//        for (Element pTag : pTags) {
//            System.out.println(pTag.text());
//        }
        //用选择器获取标签
        Elements p = document.select("p");
//        for (Element element : p) {
//            System.out.println(element.text());
//        }
       Elements sTages = document.select("a[href]");
//        for (Element sTage : sTages) {
//            System.out.println(sTage.text());
//        }
        Elements zz = document.select("a");
//        System.out.println(zz);
        //标签中的属性内容
        for (Element element : zz) {
            System.out.println(element.attr("abs:href"));
        }

    }


    public void homework(List<String> bnames) throws IOException {
        Document document = Jsoup.connect("http://www.shicimingju.com/").get();

        Elements elements = document.select(".index-li");
        Elements elementsA = elements.select("a");
        for (Element element : elementsA) {
                for (String bname : bnames) {
                    if (element.text().equals(bname)) {
                        String bookname = element.text();
                        File file = new File("/Users/dllo/Desktop/BOOK/"+bookname);
                        if (file.exists()){
                            file.delete();
                        }
                        if (!file.exists()){
                            file.mkdirs();
                        }
                        String path =file.getPath() + "/" + bookname + ".txt";
                        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
                        String url = element.attr("abs:href");
                        Document document1 = Jsoup.connect(url).get();
                        Elements elements2 = document1.select(".book-mulu");
                        Elements elementsz=   elements2.select("a[href]");
                        String bookChapter = "";
                        for (Element element1 : elementsz) {
                            bookChapter += "\n"+element1.text()+"\n";
                            String urlChapter = element1.attr("abs:href");
                            Document document2 = Jsoup.connect(urlChapter).timeout(500000).get();
                            Elements elementCodes=  document2.select("p");
                            for (Element elementCode : elementCodes) {
                                bookChapter += elementCode.text();
                            }
                        }
                        String result = bookname + bookChapter;
                        writer.write(result);
                        writer.flush();
                        writer.close();
                    }}}}


    public static void main(String[] args) throws IOException {
        List<String> bnames = new ArrayList<>();
        bnames.add("红楼梦");
        bnames.add("西游记");
        bnames.add("三国演义");
        bnames.add("水浒传");
        new MainTest().homework(bnames);
    }


@Test
    public void bingIO() throws Exception {
    SSLController.trustAll();
    String url = "https://bing.ioliu.cn/";
    Document documen =
            Jsoup.connect(url).
            userAgent("Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11").timeout(500000).
            get();
         Elements elements1 = documen.select(".download");
    for (int i = 0; i < elements1.size(); i++) {
        Element element = elements1.get(i);
        String imgURL = element.attr("abs:href");

       String a []= imgURL.split("/");
        String resultA =a[4];
        int index = resultA.indexOf("?");
        String result = resultA.substring(0, index);
        File docFile = new File("/Users/dllo/Desktop/BOOK/"+result+".jpg");
        FileUtils.copyURLToFile(new URL(imgURL), docFile);

    }

}

@Test
    public  void ZH() throws IOException {
    String url = "https://www.zhihu.com/explore/recommendations";
    Document document = Jsoup.connect(url).
            userAgent("Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11").timeout(500000).
            get();
    Elements elements1 = document.select(".question_link");
    Elements elements = elements1.select("a");

    String a = "";

    for (Element element : elements) {
        a += element.text();
        String txURL= element.attr("abs:href");
        Document document1 = Jsoup.connect(txURL).timeout(500000).
                get();

        Elements elemen = document1.select(".RichContent-inner");
        for (Element element1 : elemen) {
            String b = "";
            Elements elements2 = element1.select("p");
            Elements elements3 = element1.select("figure");
            Elements elements4 = elements3.select("img");
            for (Element element2 : elements2) {
                b += element2.text();
            }
            for (Element element2 : elements4) {
                String imgurl = element2.attr("src");
                b += "\n"+"![]("+imgurl+")"+"\n";
            }
            a += b;
        }
    }
    //版本控制
    // 564

    BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/dllo/Desktop/BOOK/test.md"));
    writer.write(a);
    writer.flush();
    writer.close();



}





}









